﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

using Ivy.FeedbackModule.Entity;
using Ivy.FeedbackModule.IBusinessLogic;
using Ivy.FeedbackModule.BusinessLogic;
using Ivy.FeedbackModule.BusinessEntity;

using Ivy.Common.Framework.GeneralUtility;

namespace DasIdeen.Pages
{
    public partial class SiteAdminListVisitorFeedbacks : System.Web.UI.Page
    {
        #region  Variable & constant
        #endregion  Variable & constant

        #region Constructor & Destructor
        #endregion Constructor & Destructor

        #region Properties
        #endregion Properties

        #region Public Methods
        #endregion Public Methods

        #region Events
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadContentFeedbackList();
                
                
            }
        }

        protected void btnDownload_Click(object sender, EventArgs e)
        {
            List<Feedbacks> lFeedbacks = new List<Feedbacks>();
            FeedbackModuleBus lFeedbackModuleBus = new FeedbackModuleBus();
            lFeedbacks = lFeedbackModuleBus.GetAllUserFeedback();

            if (lFeedbacks.Count > 0)
            {
                string header = @"""FeedbackUserKey"",""FeedbackText"",""FeedbackDate""";

                StringBuilder sb = new StringBuilder();
                sb.AppendLine(header);
                foreach (Feedbacks lFeedback in lFeedbacks)
                {
                    sb.AppendLine(string.Join(",",
                        
                        string.Format(@"""{0}""", lFeedback.FeedbackUserName),
                        string.Format(@"""{0}""", lFeedback.FeedbackUserEmail),
                        string.Format(@"""{0}""", lFeedback.FeedbackText),
                        string.Format(@"""{0}""", lFeedback.FeedbackDate)));
                        //string.Format(@"""{0}""", lSys_ErrorLog.ErrorMessage),
                        //string.Format(@"""{0}""", lSys_ErrorLog.StackTrace),
                        //string.Format(@"""{0}""", lSys_ErrorLog.Sys_DateOfCreation)));
                }
                HttpContext context = HttpContext.Current;
                context.Response.Write(sb.ToString());
                context.Response.ContentType = "text/csv";
                context.Response.AddHeader("Content-Disposition", "attachment; filename=Sys_ErrorLogDetails.csv");
                context.Response.End();
            }
        }

        protected void lnkFeedbackId_Click(object sender, EventArgs e)
        {
            LinkButton btndetails = sender as LinkButton;
            GridViewRow gvrow = (GridViewRow)btndetails.NamingContainer;
            this.lblFeedbackUserName.Text = gdvContentFeedbackList.DataKeys[gvrow.RowIndex].Value.ToString();
            this.lblFeedbackUserEmail.Text = gvrow.Cells[1].Text;
            this.lblFeedbackText.Text = gvrow.Cells[2].Text;
            this.lblFeedbackDate.Text = gvrow.Cells[3].Text;
            this.ModalPopupExtender1.Show();

        }
        protected void gvdFeedbackList_DataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowIndex < 0)
                return;

            int _myColumnIndex = 2;   // Substitute your value here

            for (_myColumnIndex = 0; _myColumnIndex <= 2; _myColumnIndex++)
            {
                string text = e.Row.Cells[_myColumnIndex].Text;

                if (text.Length > 30)
                {
                    e.Row.Cells[_myColumnIndex].Text = text.Substring(0, 30) + "...";
                }
            }

        }
        #endregion Events

        #region Private Methods
        private void CheckUserAutherization()
        {
        }

        private void ClearScreen()
        {
        }

        private void InitializeScreen()
        {
            ClearScreen();
            FillLoadLookUpData();
        }

        private void FillLoadLookUpData()
        {
        }

        private void TranslateEntityToUIControls()
        {
        }

        private void TranslateUIControlsToEntity()
        {
        }

        private void LoadContentFeedbackList()
        {
            try
            {
                List<Feedbacks> lFeedbacks = new List<Feedbacks>();
                FeedbackModuleBus lFeedbackModuleBus = new FeedbackModuleBus();
                lFeedbacks = lFeedbackModuleBus.GetAllUserFeedback();

                if (lFeedbacks.Count > 0)
                {
                    this.gdvContentFeedbackList.DataSource = lFeedbacks;
                    this.gdvContentFeedbackList.DataBind();
                }
                else
                {
                    this.lblMessage.Text = "No Records Found..!";
                }  
            }
            catch (Exception ex)
            {
                ExceptionUtility.ExceptionHandler(ex, Server.MapPath("~/"));
            }
        }

        protected void gdvContentFeedbackList_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.gdvContentFeedbackList.PageIndex = e.NewPageIndex;
            LoadContentFeedbackList();
        }
        #endregion Private Methods

        //protected void lnkFeedbackId_Click(object sender, EventArgs e)
        //{
        //    LinkButton btndetails = sender as LinkButton;
        //    GridViewRow gvrow = (GridViewRow)btndetails.NamingContainer;
        //    this.lblFeedbackKey.Text = gdvContentFeedbackList.DataKeys[gvrow.RowIndex].Value.ToString();
        //    this.lblFeedbackUserName.Text = gvrow.Cells[1].Text;
        //    this.lblFeedbackUserEmail.Text = gvrow.Cells[2].Text;
        //    this.lblFeedbackText.Text = gvrow.Cells[3].Text;
        //    this.lblFeedbackDate.Text = gvrow.Cells[4].Text;
        //    this.ModalPopupExtender1.Show();
        //}

    }
}